package com.kkd.kkdapp.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.http.entity.MyExperienceGoldActiveItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyExperienceActiviteAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater inflater;
	private List<MyExperienceGoldActiveItem> list;

	public MyExperienceActiviteAdapter(Context context,
			List<MyExperienceGoldActiveItem> list) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.experiencr_list_item,
					parent, false);
			holder.mImageView = (ImageView) convertView
					.findViewById(R.id.image);
			holder.down_image = (ImageView) convertView
					.findViewById(R.id.down_img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.mImageView.setImageResource(R.drawable.home_ad);
		}
		ImageLoader.getInstance().displayImage(list.get(position).getImage(),
				holder.mImageView, R.drawable.home_ad);
		holder.mImageView.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				if (!TextUtils.isEmpty(list.get(position).getUrl())) {
					Intent intent = new Intent(mContext, KKDWebActivity.class);
					intent.putExtra("titleName", list.get(position).getTitle());
					intent.putExtra("url", list.get(position).getUrl());
					intent.putExtra("desc", list.get(position).getDesc());
					intent.putExtra("useTitleName", true);
					mContext.startActivity(intent);
				}
			}
		});
		holder.mImageView.setOnTouchListener(new MyOnTouch(holder.down_image));
		return convertView;
	}

	class ViewHolder {
		ImageView mImageView, down_image;
	}

	class MyOnTouch implements OnTouchListener {

		ImageView img;

		public MyOnTouch(ImageView img) {
			this.img = img;
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {

			case MotionEvent.ACTION_DOWN: // 按下
				img.setVisibility(View.VISIBLE);
				break;

			case MotionEvent.ACTION_UP: // 抬起
			case MotionEvent.ACTION_OUTSIDE:
			case MotionEvent.ACTION_CANCEL:

				img.setVisibility(View.GONE);

				break;

			}
			return false;
		}
	}

}
